//
//  Category_NameBaseView.m
//  MindBox
//
//  Created by MS on 16-1-5.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Category_NameBaseView.h"
#import "UIImage+Image.h"
#import "UIView+Frame.h"
#import "LGButton.h"
#define COLOR [UIColor colorWithRed:0.98f green:0.98f blue:0.98f alpha:1]

@interface Category_NameBaseView ()

@property (nonatomic,strong) NSMutableArray * btnArray;
@property (nonatomic,weak) LGButton * selectedBtn;
@property (nonatomic,weak) UIView * lineView;
@end

@implementation Category_NameBaseView
-(NSMutableArray *)btnArray
{
    if (_btnArray==nil) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}
-(void)setCategory_names:(NSArray *)category_names
{
    _category_names = category_names;
    
    int i=0;
    for (Category_Name * cate_name in category_names) {
        
        [self setUpOneBtn:cate_name.name tag:i];
        i++;
    }

}

-(void)setUpOneBtn:(NSString *)title tag:(int)tag
{
     LGButton * btn = [LGButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:btn];
    btn.titleLabel.font = [UIFont systemFontOfSize:11];
    btn.tag =tag;
    [self.btnArray addObject:btn];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [btn setBackgroundImage:[UIImage imageWithColor:COLOR] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    if (tag==0) {
        btn.selected = YES;
        self.selectedBtn = btn;
        btn.slipHidden = NO;
        btn.diviHidden = YES;
        
        [self selectBtnName:btn];

    }
}

-(void)btnClick:(LGButton *)btn
{
    _selectedBtn.selected = NO;
    _selectedBtn.slipHidden = YES;
    _selectedBtn.diviHidden = NO;
    
    btn.selected = YES;
    btn.slipHidden = NO;
    btn.diviHidden = YES;
    self.selectedBtn = btn;
    
    [self selectBtnName:btn];
}

-(void)selectBtnName:(LGButton *)btn
{
    
    if ([self.delegate_name respondsToSelector:@selector(category_NameBaseViewDidClick:categoryName:index:)]) {
        [self.delegate_name category_NameBaseViewDidClick:self categoryName:self.category_names[btn.tag] index:btn.tag];
    }
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        self.showsVerticalScrollIndicator=NO;
        self.bounces = NO;
        self.backgroundColor =COLOR;
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(self.width-1, 0, 1, self.height)];
        _lineView = view;
        view.backgroundColor = [UIColor colorWithRed:0.86f green:0.86f blue:0.86f alpha:1.00f];

        [self addSubview:view];
    
    }
    return self;
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    _lineView.frame =CGRectMake(self.width-1, 0, 1, [UIScreen mainScreen].bounds.size.height);
    
    int i =0;
    CGFloat w = self.width;
    CGFloat h = w*0.5;//35;
       for (LGButton * btn in self.btnArray) {
        btn.frame = CGRectMake(0, h * i, w, h);
        if (i==self.btnArray.count-1) {
            
            self.contentSize  = CGSizeMake(0, CGRectGetMaxY(btn.frame));
        }
        i++;
    }
}

@end
